import {
    SET_COURSES,
    SET_COURSE,
    SET_COURSE_DETAIL,
    SET_PLUS_COURSE,
    SET_FEATURED_COURSE,
    SET_SELF_COURSE
} from '../mutation-types';
import api from '../../api';

import { forACdate } from '../../tools.js';


const state = {
    courses: [],
    course_detail: [],
    plus_course: [],
    featured_course: [],
    self_course: []
}

const mutations = {
    [SET_COURSES](state, data) {
        state.courses = forACdate(data);
        // console.log(forACdate(data))

    },
    [SET_COURSE](state, data) {
        state.courses = data;
    },
    [SET_COURSE_DETAIL](state, data) {
        state.course_detail = data;

        if (data !== null) {
            state.course_detail.star1 = Math.floor(data.grade / 2);
            state.course_detail.star2 = Math.ceil(data.grade / 2) - Math.floor(data.grade / 2);
            state.course_detail.star3 = 5 - Math.ceil(data.grade / 2);
        }

    },
    [SET_PLUS_COURSE](state, { data, status }) {
        state.plus_course = data;
        state.plus_course = {...state.course, status: status };
    },
    [SET_FEATURED_COURSE](state, data) {
        state.featured_course = data;
    },
    [SET_SELF_COURSE](state, data) {
        state.self_course = data
    }



}

const actions = {
    //获取全部课程
    GET_COURSES: ({ commit, dispatch }, date) => {
        //  dispatch('CHANGE_LOADING_SHOW').then(
        api.getallCourse(date).then(response => {
                // console.log(response.data)
                commit(SET_COURSES, response.data);
                // debugger;
                //  dispatch('HIDDEN_LOADING_SHOW')
            })
            //  )
    },
    //获取筛选课程
    GET_COURSE: ({ commit, dispatch }, uri) => {
        //  console.log(uri);
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.getCourse(uri).then(response => {
                //  console.log(response.data);
                commit(SET_COURSE, response.data);
                // debugger;
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            // )
    },

    //课程介绍
    GET_COURSE_DETAIL: ({ commit, dispatch }, id) => {
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.getCourseDetail(id).then(response => {
                commit(SET_COURSE_DETAIL, response.data);


                // dispatch('HIDDEN_LOADING_SHOW')
            })
            // )
    },


    //添加课程
    PLUS_COURSE: ({ commit, dispatch }, id) => {
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.plusCourse(id).then(response => {
                //  console.log(response.data);
                commit(SET_PLUS_COURSE, { data: response.data, status: response.status });
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            // )
    },
    //精选课程
    GET_FEATURED_COURSE: ({ commit, dispatch }) => {
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.getFeaturedCourse().then(response => {
                //  console.log(response.data);
                commit(SET_FEATURED_COURSE, response.data);
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            // )
    },
    //自选课程
    GET_SELF_COURSR: ({ commit, dispatch }, date) => {
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.getCourseInfo(date).then(response => {
                //  console.log(response.data);
                commit(SET_SELF_COURSE, response.data);
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            // )
    },
}


export default {
    state,
    actions,
    mutations
}